<?php

class ErrorCodeTool extends Exception {

    const ERROR_PARAM = -1;
    const ERROR_SUCCESS = 0;
    const ERROR_PHONE_NOT_EMOPTY = 1000;
    const ERROR_PHONE_FOREMAT = 1001;
    const ERROR_PHONE_HAS_REGISTERED = 1002;
    const ERROR_IMG_CODE_NOT_EMPTY = 1003;
    const ERROR_IMG_CODE = 1004;
    const ERROR_PASSWORD_FORMAT = 1005;
    const ERROR_PASSWORD_LENTH_TOO_SMALL = 1006;
    const ERROR_PASSWORD_LENTH_TOO_BIG = 1007;
    const ERROR_CODE_TIME_LIMIT = 1008;
    const ERROR_CODE_PHONE_COUNT_LIMIT = 1009;
    const ERROR_CODE_SEND_FAILED = 1010;
    const ERROR_TWICE_PWD_NOT_SAME = 1011;
    const ERROR_CODE = 1012;
    const ERROR_REGISTER_FAILED = 1013;
    const ERROR_PGONE_NOT_REGISTER = 1014;
    const ERROR_PASSWORD = 1015;
    const ERROR_LOGIN_FAILED = 1016;
    const ERROR_PASSWORD_RESET_FAILED= 1017;
    const ERROR_USER_NOT_LOGIN= 1018;
    
    const ERROR_OPERATION_FAILED= 2000;
    
    //小说相关的错误码 3000开头
    const ERROR_CHAPTER_HAS_PAYED = 3000;
    const ERROR_CHAPTER_NOT_EXIST = 3001;
    const ERROR_CHAPTER_PAY_FAILED = 3002;
    
    private static $_exceptions = array(
        self::ERROR_PARAM => '请求错误',
        self::ERROR_SUCCESS => 'success',
        self::ERROR_PHONE_NOT_EMOPTY => '手机号不能为空',
        self::ERROR_PHONE_FOREMAT => '手机号输入有误，请重试',
        self::ERROR_PHONE_HAS_REGISTERED => '该手机号已注册，请尝试直接登录',
        self::ERROR_IMG_CODE => '图片验证码输入有误，请重试',
        self::ERROR_PASSWORD_FORMAT => '长度为6-18个字符,需要同时包含字母和数字',
        self::ERROR_PASSWORD_LENTH_TOO_SMALL => '密码长度太短',
        self::ERROR_PASSWORD_LENTH_TOO_BIG => '密码长度太长',
        self::ERROR_CODE_TIME_LIMIT => '60秒内只允许请求一次',
        self::ERROR_CODE_PHONE_COUNT_LIMIT => '请求频繁，同一手机号当天只能请求6次',
        self::ERROR_CODE_SEND_FAILED => '短信验证码发送失败',
        self::ERROR_TWICE_PWD_NOT_SAME => '两次密码不一致',
        self::ERROR_CODE => '验证码错误',
        self::ERROR_REGISTER_FAILED => '注册失败',
        self::ERROR_PGONE_NOT_REGISTER => '该手机号没有注册，请尝试直接注册',
        self::ERROR_PASSWORD => '密码输入有误，请重试',
        self::ERROR_LOGIN_FAILED => '登录失败',
        self::ERROR_PASSWORD_RESET_FAILED => '重置密码失败',
        self::ERROR_OPERATION_FAILED => '操作超时',
        self::ERROR_USER_NOT_LOGIN => '未登录',
        self::ERROR_CHAPTER_HAS_PAYED => '该章已付费',
        self::ERROR_CHAPTER_NOT_EXIST => '该章不存在',
        self::ERROR_CHAPTER_PAY_FAILED => '购买失败',
    );

    public function __construct($code, $msg = 'unknown') {/* {{{ */
        $msg = isset(self::$_exceptions[$code]) ? self::$_exceptions[$code] : $msg;
        parent::__construct($msg, $code);
    }

    public static function getMsg($code = 10000) {
        return isset(self::$_exceptions[$code]) ? self::$_exceptions[$code] : '错误, 请重试!';
    }

}
